﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using DragDraw.Util;
using DragDraw.UserService;

namespace DragDraw
{
	public partial class SettingControl : UserControl, IUserServiceListener, ISpaceServiceListener
	{
		public SettingControl()
		{
			// Required to initialize variables
			InitializeComponent();
		}

        public void Init()
        {
            UserInfo user = OperationManager.Instance.MyUserInfo;
            SpaceInfo space = OperationManager.Instance.MySpace;
            txtNickname.Text = user.Realname;
            txtBackground.Text = space.BackgroundImage;
        }

        public void OnSetUserInfo(SetUserInfoCompletedEventArgs e)
        {
            if (e.Result == true)
                OperationManager.Instance.CurrentSpaceManager.ShowModalDialog(null);
            else
                ErrorManager.Instance.ShowWarnMessage("用户信息保存失败");
        }
        public void OnGetUserInfo(GetUserInfoCompletedEventArgs e) { }
        public void OnSetPassword(SetPasswordCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                ErrorManager.Instance.ShowWarnMessage("密码保存失败");
            }
        }
        public void OnGetUserList(GetUserListCompletedEventArgs e) { }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            UserInfo user = OperationManager.Instance.MyUserInfo;
            SpaceInfo space = OperationManager.Instance.MySpace;
            user.Realname = txtNickname.Text;
            space.BackgroundImage = txtBackground.Text;
            space.ThemeColor = ColorPanel.CurrentColor.ToString();
            //set spaceinfo
            OperationManager.Instance.SetMySpaceInfo(space);
            //set userinfo
            OperationManager.Instance.SetMyUserInfo(user, txtPassword.Password);
            if (!string.IsNullOrEmpty(txtPassword.Password))
                ServiceManager.Instance.SetPassword(user.UserID, txtPassword.Password, this);
            // tell if it is successful
        }

        private void btnUserPanel_Click(object sender, RoutedEventArgs e)
        {
            UserPanel.Visibility = Visibility.Visible;
            SpacePanel.Visibility = Visibility.Collapsed;
            AccountPanel.Visibility = Visibility.Collapsed;
        }

        private void btnSpacePanel_Click(object sender, RoutedEventArgs e)
        {
            UserPanel.Visibility = Visibility.Collapsed;
            SpacePanel.Visibility = Visibility.Visible;
            AccountPanel.Visibility = Visibility.Collapsed;
        }

        private void btnAccountPanel_Click(object sender, RoutedEventArgs e)
        {
            UserPanel.Visibility = Visibility.Collapsed;
            SpacePanel.Visibility = Visibility.Collapsed;
            AccountPanel.Visibility = Visibility.Visible;
        }

        public void OnGetSpaceInfo(GetSpaceInfoCompletedEventArgs e) { }
        public void OnSetSpaceInfo(SetSpaceInfoCompletedEventArgs e) { }
	}
}